package com.nstar.pim.model
{
	import mx.collections.ArrayCollection;
	import mx.controls.HorizontalList;
	import mx.controls.TileList;
	import mx.rpc.IResponder;

	public class TemplatesResponder implements IResponder
	{
		private var tile:TileList;
		private var hlist:HorizontalList;
		
		private var _typeList:Array = new Array();
		private var _listTemplate:Array = new Array();
		
		public function TemplatesResponder(hlist:HorizontalList,tile:TileList)
		{
			this.hlist = hlist;
			this.tile = tile;
		}

		public function result(data:Object):void
		{
			//数据分类
			var list:ArrayCollection = data.result.patterns.pattern;
			for (var i:int = 0;i<list.length;i++){
				var type:String = list.getItemAt(i).type;
				if(_typeList.indexOf(type)==-1){
					_typeList.push(type);
				}
				
				var url:String = list.getItemAt(i).url;
				var p1:int = url.lastIndexOf("/");
				var p2:int = url.indexOf(".");
				
				list.getItemAt(i).schemeName = url.substr(p1+1,p2-p1-1);
				
			}
			
			for each(var type:String in _typeList){
				var ary:Array = new Array();
				for (var i:int = 0;i<list.length;i++){
					var t:String = list.getItemAt(i).type;
					if(t == type){
						ary.push(list.getItemAt(i));
					}
				}
				_listTemplate.push(ary);
			}
			
			this.hlist.dataProvider = _typeList;
			this.hlist.selectedIndex = 0;
			this.tile.dataProvider = _listTemplate[0];
			
		}
		
		public function fault(info:Object):void
		{
		}
		
		public function get typeList():Array{
			return _typeList;
		}
		
		public function get listTemplate():Array{
			return _listTemplate;
		}
		
	}
}